package com.grocery.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.BusinessParams;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.FileItem;
import com.grocery.bean.Order;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class PaymentUtil {
    public static String payment(Order order) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

        // 设置商户订单号
//        model.setOutTradeNo("20150320010101001");
        model.setOutTradeNo(order.getCode());

        // 设置订单总金额
//        model.setTotalAmount("88.88");
        model.setTotalAmount(order.getMoney().toString());

        // 设置订单标题
        model.setSubject("慧慧百货商城");

        // 设置产品码
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088721061065972");

        // 设置订单附加信息
//        model.setBody("Iphone6 16G");

//        // 设置订单包含的商品列表信息
//        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
//        GoodsDetail goodsDetail0 = new GoodsDetail();
//        goodsDetail0.setGoodsName("ipad");
//        goodsDetail0.setQuantity(1L);
//        goodsDetail0.setPrice("2000");
//        goodsDetail0.setGoodsId("apple-01");
//        goodsDetail0.setGoodsCategory("34543238");
//        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
//        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
//        goodsDetail.add(goodsDetail0);
//        model.setGoodsDetail(goodsDetail);
//
//        // 设置业务扩展参数
//        ExtendParams extendParams = new ExtendParams();
//        extendParams.setSysServiceProviderId("2088511833207846");
//        model.setExtendParams(extendParams);

//        // 设置商户传入业务信息
//        BusinessParams businessParams = new BusinessParams();
//        businessParams.setMcCreateTradeIp("127.0.0.1");
//        model.setBusinessParams(businessParams);
//
//        // 设置可打折金额
//        model.setDiscountableAmount("80.00");
//
//        // 设置商户门店编号
//        model.setStoreId("NJ_001");

//        // 设置商户操作员编号
//        model.setOperatorId("yx_001");
//
//        // 设置商户机具终端编号
//        model.setTerminalId("NJ_T_001");
//
//        // 设置商户的原始订单号
//        model.setMerchantOrderNo("20161008001");

        request.setBizModel(model);
        request.setNotifyUrl("http://4d23e049.r8.cpolar.top/order/paymentsuccess");
        // request.setNotifyUrl();
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(response)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ;
        System.out.println(response.getBody());


        if (response.isSuccess()) {
            String body = response.getBody();
            JSONObject obj=JSON.parseObject(body);
            obj = obj.getJSONObject("alipay_trade_precreate_response");
            String qr_code=obj.getString("qr_code");
            System.out.println("调用成功");
            return qr_code;
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
             String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
             System.out.println(diagnosisUrl);
             return null;
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDWy1eCRu5dPU3cXY6I4YDZ8/2+xOTVi4nYyC5rKOfN/8rTx+tULoqMCjVqxHVh4YlBeMYBAjkq1Qv5FAxPra+87STjzSj0yFb/v1lUg9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gY/LLAyZNG8xgWcipPdqmDQMsIA/I25cxMCjRwJtSkuzfsECgYB73UkU63zSdK3yu8K+HyfyBAtfKUmgPROcE7P9yq1P89NCysP8P9Ip26UMroYoCGmaiga4mDiHHQy8zoUSFc+xMACeZSQCW1iZx2vqoI7vzHFkP4XE2X9TtxJUiwsiDENWiFSwuVcopHbkhJrd550/vLxB+tC0B15n1R4MK2acIQKBgQC6XilzMmL+zSwpcYs/aHjw62RZGYdpQNr3akc5CmDjt3L1x8m17JajFFgMaWpodrTIxI2HtYXjYnR+qgkDKF5Vx7lwkplSlcI1WhGBZ/AXDI+h3CIeV5FuBcdnRb88kUqXqeBTxLP6AVF/8kRdaw18v5R4MCDXP2eLrTL6w2fb2Q==";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApQqAiA3ZcbGTlPJPQhlQnT1ZABLCJ2DWWtuEHrp6c77Vy3wfRRCtkUjcsTXulqv5yopaviqfu+oLTKpX5qrhs/Z12DqMmZSrg2sKTlo4Pk5wXflwIyaUVh6HywKbdL2AojE3FHUzwst7C+7/12lEZyJUo/7vYqGt97MLI0BkHyRnrfNbtdSPyeSD7UsXdXyNd+IEJ903dYPdj+zCIqEPxp6fryQNlPo0XJhGKKmQVzt0Pj+AEqQbw73qGgdxbU1oWqlMDV/OzqMPZAn0OdNxt7TIlarbYmQ8JlNU04DkemKfZxMP/5T+Sk68iSWYaerYCf7yDEWNb5eL6I0PJgDiTQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000146601344");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }

}
